package ui.component
{
	import tool.tweener.Tweener;
	
	import events.GameEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	
	
	public class MultScrollBar extends Sprite
	{
		public  var sideArea:uint;
		private var _bitmapDatas:Dictionary;
		private var _bitmapDataTotal:BitmapData;
		private var _xml:XML;
		
		private var _typeButtons:Vector.<Sprite>;
		
		private var tieHolder:Sprite;
		private var side_R:Bitmap;
		
		private var _itemsParent:Sprite;
		private var _itemsHolder:Sprite;
		private var _itemWid:int = 116;
		private var _itemHei:int = 116;
		private var _itemVgap:int = 6;
		
		/////////////////////////////////////
		private var _currentType:uint = 0;
		private var _currentID:int = 0;
		private var _connectorID:int = -1;
		
		private var _equipMenu:EquipMenu = new EquipMenu;
		private var _scrollBarList:Vector.<ScrollBar> = new Vector.<ScrollBar>;
		private var _paraRollout:ParameterRollout;
		private var _robotInfo:RobotInfo;
		
		private var _activeType:Bitmap;
		private var _activeItem:Sprite;
		
		private var _markMouseX:int;
		private var _markMouseY:int;
		
		private var _markItemY:int;
		
		private var _vY:Number;
		private var _v0Y:Number;
		
		private var _fadeTime:Number = 0.4;

		//////////////////////////////////////////////////////////////////////////
		override public function get width():Number
		{
			var wid:Number = tieHolder.scaleX+_bitmapDatas["typeBar"].width+side_R.width*2;
			
			return wid;
		}
		public function get robotInfo():RobotInfo
		{
			return _robotInfo;
		}
		public function get paraRollout():ParameterRollout
		{
			return _paraRollout;
		}

		public function get connectorID():int
		{
			return _connectorID;
		}

		public function get equipMenu():EquipMenu
		{
			return _equipMenu;
		}

		public function get scrollBarList():Vector.<ScrollBar>
		{
			return _scrollBarList;
		}

		public function get id():int
		{
			return _currentID;
		}
		
		public function get type():uint
		{
			return _currentType;
		}
		
		//////////////////////////////////////////////////////////////////////////
		public function MultScrollBar(BitmapDatas:Dictionary,BitmapDataTotal:BitmapData,xml:XML,sideArea:uint=4)
		{
			_bitmapDatas = BitmapDatas;
			_bitmapDataTotal = BitmapDataTotal;
			_xml = xml;
				
			this.sideArea = sideArea;
			x = sideArea;
			y = Global.stage.stageHeight-122-sideArea;
			
			_itemsParent = new Sprite;
			_itemsHolder = new Sprite;
			_itemsParent.addChild(_itemsHolder);
			
			initUI();
			
			_equipMenu.addEventListener(GameEvent.ITEM_CALL_EQUIP,onEquipCalled);
			_equipMenu.addEventListener(GameEvent.ITEM_CALL_INSTALL,onInstallCalled);
			_equipMenu.addEventListener(GameEvent.ITEM_CALL_UNINSTALL,onUninstallCalled);
			_equipMenu.addEventListener(GameEvent.ITEM_CALL_CONNECT,onConnectCalled);
			_equipMenu.addEventListener(GameEvent.ITEM_CALL_INCREASE,onIncreaseCalled);
			_equipMenu.addEventListener(GameEvent.ITEM_CALL_SELL,onSellCalled);
		}

		public function addItem(type:uint,Icon:EquipIcon):void
		{
			if(type>_scrollBarList.length-1)
			for(var i:uint=0;i<type;i++)
			{
				try{_scrollBarList[i]}
				catch(e:Error)
				{
					var bar:ScrollBar = new ScrollBar;
					
					bar.y = (i)*(_itemVgap+_itemHei);
					
					if(tieHolder)bar.scrollWidth = tieHolder.scaleX;
					
					_scrollBarList.push(bar);
					_itemsHolder.addChild(bar);
					
					bar.currentID = -1;
					
					bar.addEventListener(GameEvent.ITEM_CALL_SELECT,onItemSelect);
					bar.addEventListener(GameEvent.ITEM_CALL_MENU,onItemCallMenu);
				}
			}
			
			_scrollBarList[type-1].addIcon(Icon);
		}
		
		private function onEquipCalled(e:GameEvent):void
		{
			dispatchEvent(new GameEvent(GameEvent.ITEM_CALL_EQUIP));
		}
		private function onInstallCalled(e:GameEvent):void
		{
			dispatchEvent(new GameEvent(GameEvent.ITEM_CALL_INSTALL));
		}
		private function onUninstallCalled(e:GameEvent):void
		{
			dispatchEvent(new GameEvent(GameEvent.ITEM_CALL_UNINSTALL));
		}
		private function onConnectCalled(e:GameEvent):void
		{
			_connectorID = _equipMenu.connectorID;
			dispatchEvent(new GameEvent(GameEvent.ITEM_CALL_CONNECT));
		}
		private function onIncreaseCalled(e:GameEvent):void
		{
			dispatchEvent(new GameEvent(GameEvent.ITEM_CALL_INCREASE));
		}
		private function onSellCalled(e:GameEvent):void
		{
			dispatchEvent(new GameEvent(GameEvent.ITEM_CALL_SELL));
		}
		
		private function onItemSelect(e:GameEvent):void
		{
			try{Global.stage.removeChild(_equipMenu);}catch(e:Error){}
			
			_currentID = _scrollBarList[_currentType].currentID;
			
			for each(var scrollBar:ScrollBar in _scrollBarList)
				if(scrollBar != _scrollBarList[_currentType])scrollBar.currentID = -1;//在ScrollBar中，如果这次的选择的id与当前id一样是不会发送选择事件的，所以在切换类型时将其它类型的id都改成-1
			
			for each(var o:EquipIcon in _scrollBarList[_currentType].members)
				try{o.removeChild(_activeItem);}catch(e:Error) {}
				
			_activeItem.scaleX = _activeItem.scaleY = 0;	
			_scrollBarList[_currentType].members[_currentID].addChild(_activeItem);
			
			Tweener.addTween(_activeItem, {scaleX:1, scaleY:1,time:0.2, transition:"easeoutquad" });
			
			dispatchEvent(new GameEvent(GameEvent.ITEM_CALL_SELECT));
		}
		
		private function onItemCallMenu(e:GameEvent):void
		{
			_equipMenu.x = Global.stage.mouseX-3;
			_equipMenu.y = Global.stage.mouseY-4;
			Global.stage.addChild(_equipMenu);
			
			if(type == 0)_equipMenu.playOut(0)
			else _equipMenu.playOut(1);
			
		}
		
		//////////////////////////////////////////////////////////////////////////
		
		private function initUI():void
		{
			_paraRollout = new ParameterRollout(_bitmapDatas);//初始化属性面板
			_paraRollout.height = 331;
			_paraRollout.y = -_paraRollout.height-10;
			addChild(_paraRollout);
			
			_robotInfo = new RobotInfo(_bitmapDatas);
			_robotInfo.y = -32;
			addChild(_robotInfo);
			
			for(var i:int=0;i<5;i++)
			{
				var par:ParameterValue = new ParameterValue;
				par.x = 4;
				par.y = 27+i*(par.height+1);
				
				_paraRollout.addChild(par);
				_paraRollout.paramValues.push(par);
			}
			
			_paraRollout.paramValues[0].type = 0;
			_paraRollout.paramValues[0].title = "装备等级:";
			
			_paraRollout.paramValues[1].type = 0;
			_paraRollout.paramValues[1].title = "防御力:";
			
			_paraRollout.paramValues[2].type = 0;
			_paraRollout.paramValues[2].title = "移动速度:";
			
			_paraRollout.paramValues[3].type = 1;
			_paraRollout.paramValues[3].title = "HP:";
			
			_paraRollout.paramValues[4].type = 1;
			_paraRollout.paramValues[4].title = "耐久值:";
			
			var drogPosHelper:Sprite = new Sprite;
			var typeBar:Bitmap = new Bitmap(_bitmapDatas["typeBar"]);
			addChild(drogPosHelper);
			/////////////////////////////////////////////////////////类型切换按钮
			_typeButtons = new Vector.<Sprite>;
			for each(var xml:XML in _xml.typeButtons.button)
			{
				var button:Sprite = new Sprite;
				
				var x:int = int(xml.@x);
				var y:int = int(xml.@y);
				var wid:int = int(xml.@wid);
				var hei:int = int(xml.@hei);
				
				var subbmd:BitmapData = new BitmapData(wid,hei,true,0);
				subbmd.copyPixels(_bitmapDataTotal,new Rectangle(x,y,wid,hei),new Point);
				
				button.x = x;
				button.y = y;
				button.addChild(new Bitmap(subbmd));
				_typeButtons.push(button);
				addChild(button);
				
				button.addEventListener(MouseEvent.CLICK,typeClick);
			}
			_activeType = new Bitmap(_bitmapDatas["activeType"]);
			_activeType.x = _typeButtons[0].x;
			_activeType.y = _typeButtons[0].y;
			_activeType.blendMode = BlendMode.ADD;
			addChild(_activeType);
			
			_activeItem = new Sprite;
			var _activeItemIcon:Bitmap = new Bitmap(_bitmapDatas["activeObject"]);
			_activeItemIcon.blendMode = BlendMode.ADD;
			
			_activeItemIcon.x = -116*0.5;
			_activeItemIcon.y = -116*0.5;
			
			_activeItem.addChild(_activeItemIcon);
			/////////////////////////////////////////////////////////
			var side_L:Bitmap = new Bitmap(_bitmapDatas["side"]);
			side_L.x = typeBar.bitmapData.width;
			
			_itemsParent.x = typeBar.bitmapData.width+side_L.bitmapData.width+1;
			_itemsParent.y = 3;
			
			tieHolder = new Sprite;
			var tie:Bitmap = new Bitmap(_bitmapDatas["tie"]);
			var tieMask:Bitmap = new Bitmap(_bitmapDatas["tie"]);
			tieHolder.x = typeBar.bitmapData.width + side_L.bitmapData.width;
			tieHolder.scaleX = (Global.stage.stageWidth - tieHolder.x - sideArea*2 - side_L.bitmapData.width);
			
			for each(var bar:ScrollBar in _scrollBarList)bar.scrollWidth = tieHolder.scaleX;
			
			_itemsParent.mask = tieMask;
			
			side_R = new Bitmap(_bitmapDatas["side"]);
			side_R.scaleX = -1;
			side_R.x = tieHolder.x+tieHolder.scaleX+side_R.bitmapData.width;
			
			drogPosHelper.addChild(typeBar);
			tieHolder.addChild(tie);
			tieHolder.addChild(tieMask);
			addChild(side_L);
			addChild(tieHolder);
			addChild(side_R);
			addChild(_itemsParent);
			/////////////////////////////////////////////////////////
			_itemsHolder.addEventListener(MouseEvent.MOUSE_DOWN,startDragItems);
			Global.stage.addEventListener(Event.RESIZE,onResize);
		}
		
		////////////////////////////////////////////////////////////////////////////////////////////////鼠标上下拖动图标来切换设备类型
		private function startDragItems(e:MouseEvent):void//标记下鼠标和道具栏的位置
		{
			_markMouseX = Global.stage.mouseX;
			_markMouseY = Global.stage.mouseY;
			
			_markItemY = _itemsHolder.y;
			
			_itemsHolder.addEventListener(MouseEvent.MOUSE_UP,mouseNotMove)//如果鼠标没动
			_itemsHolder.addEventListener(MouseEvent.MOUSE_MOVE,testDir);//如果鼠标动了
		}
		private function mouseNotMove(e:MouseEvent):void//如果鼠标没有动就抬起的话，则移除方向检查侦听
		{
			_itemsHolder.removeEventListener(MouseEvent.MOUSE_UP,mouseNotMove)
			_itemsHolder.removeEventListener(MouseEvent.MOUSE_MOVE,testDir);
		}
		
		private function testDir(e:MouseEvent):void//在第一个鼠标移动事件中检测出是不是竖移
		{
			_itemsHolder.removeEventListener(MouseEvent.MOUSE_MOVE,testDir);
			
			var h:int = Global.stage.mouseX-_markMouseX;
			var v:int = Global.stage.mouseY-_markMouseY;
			
			h = Math.abs(h);
			v = Math.abs(v);
			
			if(v>h)
			{
				_itemsHolder.removeEventListener(MouseEvent.MOUSE_DOWN,startDragItems);
				
				Global.stage.addEventListener(MouseEvent.MOUSE_UP,stopDragItems);
				_itemsHolder.addEventListener(Event.ENTER_FRAME,dragItem);
			}
		}
		
		private function dragItem(e:Event):void
		{
			_itemsHolder.y = _markItemY + Global.stage.mouseY-_markMouseY;//道具跟随鼠标Y轴
			
			if(_itemsHolder.y-_markItemY > _itemHei/2)//类型下移
			{
				_itemsHolder.removeEventListener(Event.ENTER_FRAME,dragItem);
				Global.stage.removeEventListener(MouseEvent.MOUSE_UP,stopDragItems);
				
				if(_currentType>0)//有得移就移，不行就回到原位
				{
					_currentType--;
					var tarY:int = -(_currentType*_itemVgap + _currentType*_itemHei);
					Tweener.addTween(_itemsHolder, {y:tarY, time:_fadeTime, transition:"easeoutquad" ,onComplete:resumeMouseClick});
					
				}else Tweener.addTween(_itemsHolder, {y:_markItemY, time:_fadeTime, transition:"easeoutquad",onComplete:resumeMouseClick});
				
				Tweener.addTween(_activeType,{y: _typeButtons[_currentType].y,time:_fadeTime, transition:"easeoutquad" });
				
			}else if(_itemsHolder.y-_markItemY < -_itemHei/2)//类型上移
			{
				_itemsHolder.removeEventListener(Event.ENTER_FRAME,dragItem);
				Global.stage.removeEventListener(MouseEvent.MOUSE_UP,stopDragItems);
				
				if(_currentType<_scrollBarList.length-1)//有得移就移，不行就回到原位
				{
					_currentType++;
					tarY = -(_currentType*_itemVgap + _currentType*_itemHei);
					Tweener.addTween(_itemsHolder, {y:tarY, time:_fadeTime, transition:"easeoutquad",onComplete:resumeMouseClick});
					
				}else Tweener.addTween(_itemsHolder, {y:_markItemY, time:_fadeTime, transition:"easeoutquad",onComplete:resumeMouseClick});
				
				Tweener.addTween(_activeType,{y: _typeButtons[_currentType].y,time:_fadeTime, transition:"easeoutquad" });
			}
		}
		
		private function resumeMouseClick():void
		{
			_itemsHolder.addEventListener(MouseEvent.MOUSE_DOWN,startDragItems);
		}
		
		private function stopDragItems(e:MouseEvent):void
		{
			Global.stage.removeEventListener(MouseEvent.MOUSE_UP,stopDragItems);
			_itemsHolder.removeEventListener(Event.ENTER_FRAME,dragItem);
			
			Tweener.addTween(_itemsHolder, {y:_markItemY, time:_fadeTime, transition:"easeoutquad",onComplete:resumeMouseClick});
			Tweener.addTween(_activeType,{y: _typeButtons[_currentType].y,time:_fadeTime, transition:"easeoutquad" });
		}
		
		////////////////////////////////////////////////////////////////////////////////////////////////点击图标来切换设备类型
		private function typeClick(e:MouseEvent):void
		{
			var btn:Sprite = e.currentTarget as Sprite;
			
			var time:Number = Math.abs(_currentType-_typeButtons.indexOf(btn))*_fadeTime;
			if(time>_fadeTime*2)time = _fadeTime*2;
			
			
			_currentType = _typeButtons.indexOf(btn);
			
			Tweener.addTween(_activeType,{y: _typeButtons[_currentType].y,time:time, transition:"easeoutquad" });
			
			var tarY:int = -_currentType*(_itemVgap + _itemHei);
			
			Tweener.addTween(_itemsHolder,{y:tarY ,time:time, transition:"easeoutquad" });
		}
		////////////////////////////////////////////////////////////////////////////////////////////////
		private function onResize(e:Event):void//根据舞台宽度作自适应
		{
			tieHolder.scaleX = (Global.stage.stageWidth - tieHolder.x - sideArea*2 - side_R.bitmapData.width);
			side_R.x = tieHolder.x+tieHolder.scaleX+side_R.bitmapData.width;
			for each(var bar:ScrollBar in _scrollBarList)bar.scrollWidth = tieHolder.scaleX;
			
			y = Global.stage.stageHeight-side_R.bitmapData.height-sideArea;
			
			_paraRollout.height = y-80-13;
			_paraRollout.y = -_paraRollout.height-10;
		}
		
	}
	
}

	
	
	
	
	
